//
//  ByUserMyDataVC.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/15.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByUserMyDataVC.h"

#import "ByTextView.h"

//
#import "ByIconChooseDataSource.h"

@interface ByUserMyDataVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) ByIconChooseDataSource *iconChooseDataSource;

@property (nonatomic, copy) NSArray *titles;
@property (nonatomic, copy) NSArray *placeAry;


@property (nonatomic, strong) UILabel     *sexLabel;
@property (nonatomic, strong) ByTextView  *infoView;
@property (nonatomic, strong) UITextField *nickNameTF;

@end

@implementation ByUserMyDataVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人资料";
    [self addLeftBackBarButtonItem];
    [self createViews];
}

#pragma mark - getter
- (NSArray *)titles{
    if (!_titles) {
        _titles = @[@[@"昵称",@"性别"],@[@"简介"]];
    }
    return _titles;
}

- (NSArray *)placeAry{
    if (!_placeAry) {
        _placeAry = @[@[@"填写昵称",@"未填写"],@[@"简单介绍一下自己"]];
    }
    return _placeAry;
}
- (UIImageView *)iconView {
    if (!_iconView) {
        CGFloat width = [@"更换头像" getSizeOfString:14].width;
        _iconView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - width - 20)/2, 30, width + 20, width + 20)];
        _iconView.backgroundColor = [UIColor whiteColor];
        _iconView.layer.cornerRadius = _iconView.height / 2;
        _iconView.layer.masksToBounds = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickIcon)];
        _iconView.userInteractionEnabled = YES;
        [_iconView addGestureRecognizer:tap];
//        [_iconView setImageWithURL:[NSURL URLWithString:BY_USER.head] placeholderImage:DEFAULT_IMAGE completed:nil];
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, width + 20 - 25, width + 20, 25)];
        bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.35];
        [_iconView addSubview:bgView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, width, 20)];
        label.text = @"更换头像";
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = FONT(12);
        label.adjustsFontSizeToFitWidth = YES;
        [bgView addSubview:label];
    }
    return _iconView;
}

- (ByIconChooseDataSource *)iconChooseDataSource {
    if (!_iconChooseDataSource) {
        _iconChooseDataSource = [[ByIconChooseDataSource alloc] init];
        [_iconChooseDataSource fireWithViewController:self];
    }
    return _iconChooseDataSource;
}

#pragma mark - 上传头像
- (void)clickIcon{
    [self.iconChooseDataSource makeConfigToSelectIcon];
//    __weak typeof(self) weakself = self;
    [self.iconChooseDataSource setCompleteBlock:^(UIImage *image) {
        
//        [weakself uploadIcon:image];
    }];
    
}

#pragma mark - create views
- (void)createViews{
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-40);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom).offset(-40);
        }
    }];
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    
    CGFloat width = [@"更换头像" getSizeOfString:14].width;
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, width + 20 + 60)];
    self.tableView.tableHeaderView = self.headerView;
    self.headerView.backgroundColor = BY_THEME_COLOR;
    [self.headerView addSubview:self.iconView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    UIButton *saveButton = [[UIButton alloc] init];
    [self.view addSubview:saveButton];
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(self.tableView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 40));
    }];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveButton.titleLabel.font = FONT(14);
    [saveButton setBackgroundColor:BY_THEME_COLOR];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *sectionAry = self.titles[section];
    return sectionAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentier = @"dataCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    for (UIView *subview in cell.subviews) {
        [subview removeFromSuperview];
    }
    NSArray *titleSection = self.titles[indexPath.section];
    NSArray *placeSection = self.placeAry[indexPath.section];
    // 标题
    CGFloat x,y,width,height;
    x = 10;
    y = 10;
    width = [@"昵称" getSizeOfString:14].width;
    height = 30;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    titleLabel.font = FONT(14);
    titleLabel.text = titleSection[indexPath.row];
    [cell addSubview:titleLabel];
    
    x = titleLabel.right + 30;
    y = 10;
    width = SCREEN_WIDTH - x - 10;
    height = 30;
//    ByModelUser *user = [ByCacheUser getUserInfo];
    // 昵称
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            _nickNameTF = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
            _nickNameTF.font = FONT(14);
            _nickNameTF.placeholder = placeSection[indexPath.row];
            [cell addSubview:_nickNameTF];
            //        if (user.nickname.length != 0) {
            //            _nickNameTF.text = user.nickname;
            //        }
        }else{
            _sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width - 40, height)];
            _sexLabel.font = FONT(14);
            _sexLabel.text = placeSection[indexPath.row];
            [cell addSubview:_sexLabel];
            
            UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(_sexLabel.right, 15, 20, 20)];
            img.image = [UIImage imageNamed:@"mine_arrow"];
            [cell addSubview:img];
        }
   
    }
    else{
        _infoView = [[ByTextView alloc] initWithFrame:CGRectMake(x, 10, width, 30)];
        _infoView.placeholder = placeSection[indexPath.row];
        _infoView.font = FONT(14);
        _infoView.placeholderTextColor = [UIColor lightGrayColor];
        [cell addSubview:_infoView];
//        if (user.sign.length != 0) {
//            _infoView.text = user.sign;
//        }
        
    }
    
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}


@end
